﻿using Gameserver.Biz;
using Gameserver.Cache;
using Gameserver.DAO;
using Gameserver.Tools;
using Protocol;
using Protocol.DTO;
using ServerFrame;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gameserver.Logic.Login
{
    /// <summary>
    /// 处理HanderCenter中的OnMessageRecive()方法的登陆模块
    /// </summary>
    class LoginHandler:AbsOnceHandler,IMessageHandler
    {
        UserBiz userBiz;
        RoleBiz roleBiz;
        RoleCache roleCache;
        public LoginHandler()
        {
            Logic_Type = GameProtocol.login;
            userBiz = BizFactory.userBiz;
            roleBiz = BizFactory.roleBiz;
            roleCache = CacheFactory.roleCache;
        }
        public void OnClientConnected(Mytoken token)
        {
 
        }
        public void OnClientClose(Mytoken token, string error)
        {
            EventExcuteUtil.Instance.Excute(
                () =>
                {
                    userBiz.ClientClose(token);
                });
        }
        public void OnMessageRecive(Mytoken token, TransModel model)
        {
            switch (model.command)
            {
                case LoginProtocol.Login_CREQ:
                    //从缓存去读取用户信息，并进行比较，判断是否能登陆
                    EventExcuteUtil.Instance.Excute(
                        () =>
                        {
                            User_DTO dto;
                            try
                            {
                                dto = model.GetMessage<User_DTO>();
                            }
                            catch (Exception ex)
                            {
                                dto = null;
                            }
                            if (dto == null)
                                return;
                            int result = userBiz.LoginBiz(dto, token);
                            Send(token, result, LoginProtocol.Login_SRES);
                        });
                    break;
                case LoginProtocol.Reg_CREQ:
                    EventExcuteUtil.Instance.Excute(
                        () =>
                        {
                            User_DTO dto;
                            try
                            {
                                dto = model.GetMessage<User_DTO>();
                            }
                            catch (Exception ex)
                            {
                                dto = null;
                            }
                            if (dto == null)
                                return;
                            int result = userBiz.RegBiz(dto);
                            Send(token, result, LoginProtocol.Reg_SRES);
                        });
                    break;
                case LoginProtocol.RLogin_CREQ:
                    EventExcuteUtil.Instance.Excute(
                        () =>
                        {
                            int result = roleBiz.RoleLogin(token);
                            if(result!=0)
                            {
                                Send(token, LoginProtocol.RLogin_SRES);
                            }
                            else
                            {
                                Send(token, result, LoginProtocol.RLogin_SRES, GetRoleDTO(token));
                            }
                        });
                    break;
                case LoginProtocol.RReg_CREQ:
                    EventExcuteUtil.Instance.Excute(
                        () =>
                        {
                            string name = model.GetMessage<string>();
                            int result = roleBiz.Create(token, name);
                            Send(token, result, LoginProtocol.RReg_SRES);
                        });
                    break;
                default:
                    break;
            }
        }
        Role_DTO GetRoleDTO(Mytoken token)
        {
            Role_DAO role = roleCache.GetRoleByToken(token);
            Role_DTO dto = new Role_DTO(role.role_id, role.rolename, role.coin, role.level, role.exp);
            return dto;
        }
    }
}
